export default defineNuxtRouteMiddleware((to) => {
    const userAgent = process.server
        ? useRequestHeaders()['user-agent'] ?? ''
        : navigator.userAgent
    to.params.isMobile = String(/mobile/i.test(userAgent))
    const isMobile = () => {
        return String(/mobile/i.test(userAgent))
    }
    // 若为pc端但url中包含/m或者/m，重定向到pc
    if ((to.fullPath.includes('/m') || to.fullPath.includes('/m/')) && isMobile() === 'false') {
        const url = to.fullPath.substring(2)
        return navigateTo(url)
    }
    // 若为移动端但url中不包含/m或者/m，重定向到m
    if ((!to.fullPath.includes('/m') || !to.fullPath.includes('/m/')) && isMobile() === 'true') {
        return navigateTo('/m' + to.fullPath)
    }
})
